package cn.afactory.tygz.ui.sns;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.AttributeSet;
import android.view.View;

import com.tendcloud.tenddata.TCAgent;

import cn.afactory.tygz.R;
import cn.afactory.tygz.common.UserListType;
import cn.afactory.tygz.ui.base.BaseFragmentActivity;
import cn.afactory.tygz.ui.base.utils.SPUtils;
import cn.afactory.tygz.ui.sns.fragment.UserListFragment;


/**
 * 用户列表
 */
public class UserListActivity extends BaseFragmentActivity {
    private UserListFragment mUserListFragment;

    private int mPageIndex = 0;
    private long mTargetUserId = -1;
    private String mTitle = null;
    private int mUserType = -1;
    private long mPostId;
    private int mPrasizeCount = 0;
    public void setPageIndex(int index) {
        mPageIndex = index;
    }
    private void initTitleBar() {
        mTitle = getIntent().getStringExtra(SPUtils.EXTRA_DATA);
        mTargetUserId = getIntent().getLongExtra(SPUtils.EXTRA_USER_ID, -1);
        mUserType = getIntent().getIntExtra(SPUtils.EXTRA_TYPE, -1);
        mPostId = getIntent().getLongExtra(SPUtils.EXTRA_POST_ID, -1);
        mPrasizeCount = getIntent().getIntExtra(SPUtils.EXTRA_PRASIZE_COUNT, -1);
        if(mUserType == UserListType.PRASIZE_LIST){
            setTitleText(String.format(getString(R.string.title_prasize_list),mPrasizeCount));
        }else {
            if (mTitle != null) {
                setTitleText(mTitle);
            }
        }
        showBackView();
    }

    /**
     * 跳转到用户的粉丝列表
     * @param context
     * @param targetUserId
     * @param title
     */
    public static void gotoUserFansListPage(Context context,
                                            long targetUserId,
                                            String title,
                                            long postId,
                                            int count,
                                            int type){
        Intent intent = new Intent(context,UserListActivity.class);
        if(title != null) {
            intent.putExtra(SPUtils.EXTRA_DATA, title);
        }
        if(type > 0) {
            intent.putExtra(SPUtils.EXTRA_TYPE, type);
        }
        if(targetUserId > 0) {
            intent.putExtra(SPUtils.EXTRA_USER_ID, targetUserId);
        }

        if(postId > 0) {
            intent.putExtra(SPUtils.EXTRA_POST_ID, postId);
        }
        if(count > 0) {
            intent.putExtra(SPUtils.EXTRA_PRASIZE_COUNT, count);
        }
        context.startActivity(intent);
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    @Override
    public void onResume() {
        super.onResume();
        TCAgent.onResume(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_fragment);
        initTitleBar();
        initview();
    }

    private void initview() {
        mUserListFragment = new UserListFragment(mTargetUserId,mUserType,mPostId);
        if (mUserListFragment != null) {
            addFragment(mUserListFragment);
        } else {
            finish();
        }
    }

    private void addFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fl_layout, fragment, "" + fragment.getClass())
                .commit();
    }
}